//
//  CCProgressThumb.m
//  didiAnimation
//
//  Created by iOS app dev on 17/2/21.
//  Copyright © 2017年 007qilv. All rights reserved.
//

#import "CCProgressThumb.h"

@implementation CCProgressThumb


-(instancetype)init
{
    if ((self = [super init])) {
        
        [self steupLayer];
    }
    return self;

}

- (void)layoutSublayers
{
    _timeLabel.frame = self.bounds;
    
    [_timeLabel sizeToFit];
    _timeLabel.center = CGPointMake(CGRectGetMidX(self.bounds) - _timeLabel.frame.origin.x,
                                    CGRectGetMidY(self.bounds) - _timeLabel.frame.origin.y);
}



-(void)steupLayer
{
    //绘制圆
    UIGraphicsBeginImageContext(CGSizeMake(kProgressThumbWh, kProgressThumbWh));
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, 1);
    CGContextSetFillColorWithColor(ctx, [UIColor lightGrayColor].CGColor);
    CGContextSetStrokeColorWithColor(ctx, [UIColor lightGrayColor].CGColor);
    CGContextAddEllipseInRect(ctx, CGRectMake(1, 1, kProgressThumbWh - 2, kProgressThumbWh - 2));
    CGContextDrawPath(ctx, kCGPathFillStroke);
    UIImage *circle = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    UIImageView *circleView = [[UIImageView alloc] initWithImage:circle];
    circleView.frame = CGRectMake(0, 0, kProgressThumbWh, kProgressThumbWh);
    circleView.image = circle;
    [self addSublayer:circleView.layer];
    
    
    
    _timeLabel = [[UILabel alloc] initWithFrame:self.bounds];
    _timeLabel.textColor = [UIColor redColor];
    _timeLabel.font = [UIFont systemFontOfSize:10];
    _timeLabel.textAlignment = NSTextAlignmentCenter;
    _timeLabel.text = @"00:00";
    [self addSublayer:_timeLabel.layer];
    
    
    _startAngle = - M_PI / 2;
    

}



- (void)setAnimationTime:(NSTimeInterval)animationTime
{
    _animationTime = animationTime;
}


- (double)calculatePercent:(NSTimeInterval)fromTime toTime:(NSTimeInterval)toTime
{
    double progress = 0.0f;
    if ((toTime > 0) && (fromTime > 0)) {
        progress = fromTime / toTime;
        if ((progress * 100) > 100) {
            progress = 1.0f;
        }
    }
    return progress;
}


- (void)startAnimation
{
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = YES;
    pathAnimation.duration = kAnimTimeInterval;
    pathAnimation.repeatCount = 0;
    pathAnimation.autoreverses = YES;
    
    CGMutablePathRef arcPath = CGPathCreateMutable();
    CGPathAddPath(arcPath, NULL, [self bezierPathFromParentLayerArcCenter]);
    pathAnimation.path = arcPath;
    CGPathRelease(arcPath);
    [self addAnimation:pathAnimation forKey:@"position"];
}


/**
 *  根据父Layer获取到一个移动路径
 *  @return
 */
- (CGPathRef)bezierPathFromParentLayerArcCenter
{
    CGFloat centerX = CGRectGetWidth(self.superlayer.frame) / 2.0;
    CGFloat centerY = CGRectGetHeight(self.superlayer.frame) / 2.0;
    double tmpStartAngle = _startAngle;
    _startAngle = _startAngle + (2 * M_PI) * kAnimTimeInterval / _animationTime;
    return [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY)
                                          radius:centerX
                                      startAngle:tmpStartAngle
                                        endAngle:_startAngle
                                       clockwise:YES].CGPath;
}


- (void)stopAnimation
{
    [self removeAllAnimations];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
